package com.dgut.leave.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.StringUtils;

import com.fasterxml.jackson.annotation.JsonFormat;

public class LeaveQueryDTO {
	private String userId;//用户登录名
	@DateTimeFormat(pattern = "yyyy/MM/dd HH:mm:ss")
	private Date beginDate;//开始时间
	@DateTimeFormat(pattern = "yyyy/MM/dd")
	private Date endDate;//结束时间
	@DateTimeFormat(pattern = "yyyy/MM/dd")
	private Date realityStartTime;//实际开始时间
	@DateTimeFormat(pattern = "yyyy/MM/dd")
	private Date realityEndTime;//实际结束时间
	@DateTimeFormat(pattern = "yyyy/MM/dd")
	private Date applyTime;//申请时间
	private String leaveType;// 请假类型
	private String reason;//申请理由
	private String status;//审核状态

	/** ------------流程数据-------------- **/
	/* 任务 */
	private String taskId;
	private String taskName;
	private Date taskCreateTime;
	private String assignee;
	private String taskDefinitionKey;
	/* 流程实例 */
	private String processInstanceId;
	/* 流程图定义 */
	private String processDefinitionId;
	private boolean suspended;
	private int version;

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss", timezone = "GMT+8")
	public Date getBeginDate() {
		return beginDate;
	}

	public void setBeginDate(Date beginDate) {
		this.beginDate = beginDate;
	}

	@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss", timezone = "GMT+8")
	public Date getEndDate() {
		return endDate;
	}

	@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss", timezone = "GMT+8")
	public Date getRealityStartTime() {
		return realityStartTime;
	}

	public void setRealityStartTime(Date realityStartTime) {
		this.realityStartTime = realityStartTime;
	}

	@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss", timezone = "GMT+8")
	public Date getRealityEndTime() {
		return realityEndTime;
	}

	@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss", timezone = "GMT+8")
	public Date getApplyTime() {
		return applyTime;
	}

	public void setApplyTime(Date applyTime) {
		this.applyTime = applyTime;
	}

	public void setRealityEndTime(Date realityEndTime) {
		this.realityEndTime = realityEndTime;
	}

	public String getTaskId() {
		return taskId;
	}

	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}

	public String getTaskName() {
		return taskName;
	}

	public void setTaskName(String taskName) {
		this.taskName = taskName;
	}

	public Date getTaskCreateTime() {
		return taskCreateTime;
	}

	public void setTaskCreateTime(Date taskCreateTime) {
		this.taskCreateTime = taskCreateTime;
	}

	public String getAssignee() {
		return assignee;
	}

	public void setAssignee(String assignee) {
		this.assignee = assignee;
	}

	public String getTaskDefinitionKey() {
		return taskDefinitionKey;
	}

	public void setTaskDefinitionKey(String taskDefinitionKey) {
		this.taskDefinitionKey = taskDefinitionKey;
	}

	public String getProcessInstanceId() {
		return processInstanceId;
	}

	public void setProcessInstanceId(String processInstanceId) {
		this.processInstanceId = processInstanceId;
	}

	public String getProcessDefinitionId() {
		return processDefinitionId;
	}

	public void setProcessDefinitionId(String processDefinitionId) {
		this.processDefinitionId = processDefinitionId;
	}

	public boolean isSuspended() {
		return suspended;
	}

	public void setSuspended(boolean suspended) {
		this.suspended = suspended;
	}

	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public String getReason() {
		return reason;
	}

	public void setReason(String reason) {
		this.reason = reason;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getLeaveType() {
		return leaveType;
	}

	public void setLeaveType(String leaveType) {
		this.leaveType = leaveType;
	}

	public static Specification<Leave> getSpecification(LeaveQueryDTO leaveQueryDTO) {
		Specification<Leave> spec = new Specification<Leave>() {
			public Predicate toPredicate(Root<Leave> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				// 1.Predicate查询条件集合
				List<Predicate> list = new ArrayList<Predicate>();
				// 2.根据QueryDTO数据字段的值进行判读那以及条件的组装
				// 姓名 外加查询条件是当前登录用户的userId
				Predicate p1 = cb.like(root.get("userId").as(String.class), "%" + leaveQueryDTO.getUserId() + "%");
				list.add(p1);
				// 审核状态
				if (leaveQueryDTO != null && !StringUtils.isEmpty(leaveQueryDTO.getStatus())) {

					Predicate p2 = cb.like(root.get("status").as(String.class), "%" + leaveQueryDTO.getStatus() + "%");
					list.add(p2);
				}
				// 请假类型
				if (leaveQueryDTO != null && !StringUtils.isEmpty(leaveQueryDTO.getLeaveType())) {
					Predicate p3 = cb.like(root.get("leaveType").as(String.class),
							"%" + leaveQueryDTO.getLeaveType() + "%");
					list.add(p3);
				}
				// 3.Predicate查询条件集合的 size 创建对应的Predicate查询条件数组
				Predicate[] p = new Predicate[list.size()];
				// 4.CriteriaBuilder的and 函数组装 查询条件数组
				return cb.and(list.toArray(p));
			}

		};
		return spec;
	}
}
